<template>
  <div class="cate-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div style="margin-top: 15px;">
        <el-button type="primary" @click="cateAdd">添加分类</el-button>
        <zk-table
          style="margin: 20px 0;"
          :data="cateList"
          :columns="columns"
          :selection-type="false"
          :expand-type="false"
          :show-index="true"
          border
        >
          <template slot="cat_deleted" slot-scope="scoped">
            <i v-if="scoped.row.cat_deleted" class="el-icon-error"></i>
            <i style="color:green" v-else class="el-icon-success"></i>
          </template>

          <template slot="cat_level" slot-scope="scoped">
            <el-tag v-if="scoped.row.cat_level==0">一级</el-tag>
            <el-tag v-else-if="scoped.row.cat_level==1" type="success">二级</el-tag>
            <el-tag v-else type="warning">三级</el-tag>
          </template>
        </zk-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="cateParams.pagenum"
          :page-sizes="[5, 10, 30, 40]"
          :page-size="cateParams.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <!-- 添加分类页面 -->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible">
      <el-form :model="formAddCate" :rules="rules" ref="ruleForm">
        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="cat_name">
          <el-input v-model="formAddCate.cat_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" :label-width="formLabelWidth">
          <el-cascader
            v-model="value"
            :options="options"
            :props="props"
            @change="handleChange"
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
           
<script>
import { cateList_api, cateAdd_api } from "@/api";
export default {
  data() {
    return {
      //控制面板的显示
      dialogFormVisible: false,
      formLabelWidth: "120px",
      //级联选择器数据
      value: [],
      options: [],
      props: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      //发送的参数
      formAddCate: {
        cat_pid: "",
        cat_name: "",
        cat_level: "",
      },
      //表单校验规则
      rules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      //分页
      cateParams: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      cateList: [],
      total: 1,
      //表格标题数据
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template",
          template: "cat_deleted",
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template",
          template: "cat_level",
        },
        {
          label: "操作",
          prop: "cat_id",
          type: "template",
          template: "cat_id",
        },
      ],
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    //添加分类按钮
    async cateAdd() {
      //获取数据放到级联选择器里
      const { data: res } = await cateList_api({ type: 2 });
      // console.log(res, 66666);
      this.options = res.data;
      this.dialogFormVisible = true;
      // //清除数据
      // this.$refs[formName].resetFields();
      // this.value = [];
    },
    async getCateList() {
      const { data: res } = await cateList_api(this.cateParams);
      // console.log(res, 66666);
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    // 级联选择器事件
    handleChange(value) {
      console.log(value);
    },
    //点击确定按钮 二次校验
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //获取第一个参数(value数组的最后一个)
          if (this.value.length > 0) {
            this.formAddCate.cat_pid = this.value[this.value.length - 1];
          } else {
            this.formAddCate.cat_pid = 0;
          }
          this.formAddCate.cat_level = this.value.length;
          //  发送请求
          const { data: res } = await cateAdd_api(this.formAddCate);
          // console.log(res, 3333)
          this.dialogFormVisible = false;
          this.getCateList();
          // //清除数据
          this.$refs[formName].resetFields();
          this.value = [];
        } else {
          return false;
        }
      });
    },
    /* 分页 */
    handleSizeChange(val) {
      this.cateParams.pagesize = val;
      this.getCateList();
    },
    handleCurrentChange(val) {
      this.cateParams.pagenum = val;
      this.getCateList();
    },
  },
};
</script>
<style scoped lang='less'>
.cate-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }
  .el-cascader {
    width: 100%;
  }
}
</style>